package com.bjsxt.controller.system;

import ch.qos.logback.classic.sift.AppenderFactoryUsingJoran;
import com.bjsxt.aspectj.annotation.Log;
import com.bjsxt.aspectj.enums.BusinessType;
import com.bjsxt.constants.Constants;
import com.bjsxt.domain.Menu;
import com.bjsxt.domain.Role;
import com.bjsxt.dto.MenuDto;
import com.bjsxt.service.MenuService;
import com.bjsxt.utils.ShiroSecurityUtils;
import com.bjsxt.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:liao
 * @Description:TODO 菜单controller
 * @date:2020/12/11
 */
@RestController
@RequestMapping("system/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 根据指定条件(菜单名称、状态)查询菜单信息
     * @param menuDto
     * @return
     */
    @GetMapping("listAllMenus")
    public AjaxResult listAllMenus(MenuDto menuDto){
       List<Menu> list =  menuService.listAllMenus(menuDto);
       return AjaxResult.success("查询成功",list);
    }

    /**
     * 查询有效的菜单列表
     */
    @GetMapping("selectMenuTree")
    public AjaxResult selectMenuTree(){
        //只查询可用的
        MenuDto menuDto = new MenuDto();
        menuDto.setStatus(Constants.STATUS_TRUE);
        List<Menu> menuList = menuService.listAllMenus(menuDto);
        return AjaxResult.success("查询成功",menuList);
    }

    /**
     * 添加菜单和权限信息
     */
    @Log(title = "添加菜单信息",businessType = BusinessType.INSERT)
    @PostMapping("addMenu")
    public AjaxResult addMenu(@Validated MenuDto menuDto){
        menuDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(menuService.addMenu(menuDto));
    }

    /**
     * 根据菜单id查询菜单信息
     * @param menuId
     * @return
     */
    @GetMapping("getMenuById/{menuId}")
    public AjaxResult getMenuById(@PathVariable Long menuId){
        Menu menu = menuService.getOne(menuId);
        return AjaxResult.success(menu);
    }

    /**
     * 修改菜单和权限信息
     */
    @Log(title = "修改菜单信息",businessType = BusinessType.UPDATE)
    @PutMapping("updateMenu")
    public AjaxResult updateMenu(@Validated MenuDto menuDto){
        menuDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(menuService.updateMenu(menuDto));
    }

    /**
     * 根据菜单id删除菜单信息
     */
    @Log(title = "删除菜单信息",businessType =BusinessType.DELETE)
    @DeleteMapping("deleteMenuById/{menuId}")
    public AjaxResult deleteMenuById(@PathVariable Long menuId){
        // 删除之前判断当前菜单有没有子节点
        if (menuService.hasChildByMenuId(menuId)){
            return AjaxResult.fail("当前要删除的菜单有子节点，请先删除子节点");
        }
        return AjaxResult.toAjax(menuService.deleteMenuById(menuId));
    }

    /**
     * 根据角色id查询 菜单id集合(只查询子节点的)
     */
    @GetMapping("getMenuIdsByRoleId/{roleId}")
    public  AjaxResult getMenuIdsByRoleId(@PathVariable Long roleId){
      List<Long> menuIds =  menuService.getMenuIdsByRoleId(roleId);
      return AjaxResult.success(menuIds);
    }
}
